//
//  MLSBasicTableViewController.swift
//  MLSMircoBlog
//
//  Created by 袁航 on 15/7/28.
//  Copyright © 2015年 yuanhang. All rights reserved.
//

import UIKit

// 遵守vistorview的代理协议,监听注册和登录按钮点击
class MLSBasicTableViewController: UITableViewController,MLSVistorviewDelegate {
    
    // MARK - 成员变量
    // 判断是否登录
    private let userLogin = MLSAccessToken.userLogon
    
    // 定义视图(设置未登录界面,公开,让子类访问,便于设置视图内容)
    let vistroView = MLSVistorview()
    
    // MARK: - 系统方法
    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置导航栏
        setupNaviBtn()
    }
    
    // 设置导航栏按钮
    private func setupNaviBtn() {
        // 判断用户是否登录
        if !userLogin {
            navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorviewShouldRegister")
            navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: "vistorviewShouldLogin")
            return
        }
        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftBtn)
        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightBtn)
    }
    
    // MARK:- 设置界面
    // 判断用户是否登录,加载对应的视图
    override func loadView() {
        userLogin ? super.loadView() : setupVisitorView()
    }
    
    // MARK:- 自定义方法
    // 设置未登录界面
    private func setupVisitorView() {
        // 成为代理
        vistroView.delegate = self
        view = vistroView
    }
    
    // MARK: - 代理方法
    // 登录按钮
    func vistorviewShouldLogin() {
        // modal出授权页面
        presentViewController(UINavigationController(rootViewController: MLSOAuthController(register: false)), animated: true, completion: nil)
    }
    // 注册按钮
    func vistorviewShouldRegister() {
        // modal出注册页面
        presentViewController(UINavigationController(rootViewController: MLSOAuthController(register: true)), animated: true, completion: nil)
    }
    
    // MARK:- 懒加载控件
    lazy var webView = UIWebView()
    lazy var leftBtn: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "navigationbar_friendsearch"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "navigationbar_friendsearch_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        return button
    }()
    lazy var rightBtn: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "navigationbar_pop"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "navigationbar_pop_highlighted"), forState: UIControlState.Highlighted)
        button.sizeToFit()
        return button
    }()
}
